package net.xdclass.controller;

import com.google.code.kaptcha.Producer;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import net.xdclass.enums.BizCodeEnum;
import net.xdclass.component.MailService;
import net.xdclass.enums.SendCodeType;
import net.xdclass.service.NotifyService;
import net.xdclass.util.CommonUtil;
import net.xdclass.util.JsonData;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

@Api(tags = "通知模块")
@RestController
@Slf4j
@RequestMapping("/api/notify/v1")
public class NotifyController {

    @Autowired
    private Producer captchaProducer;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private MailService mailService;
    @Autowired
    private NotifyService notifyService;

    private static final long CAPTCHE_EXPIRE_TIME = 60*1000*10;

    @ApiOperation("获取图形验证码")
    @GetMapping("/getCaptcha")
    public void getCaptcha(HttpServletRequest request, HttpServletResponse response){
        String captchaText = captchaProducer.createText();
        stringRedisTemplate.opsForValue().set(getCaptchaKey(request),captchaText,CAPTCHE_EXPIRE_TIME, TimeUnit.MILLISECONDS);
        BufferedImage image = captchaProducer.createImage(captchaText);
        ServletOutputStream servletOutputStream = null;
        try {
            servletOutputStream = response.getOutputStream();
            ImageIO.write(image,"jpg",servletOutputStream);
            servletOutputStream.flush();
            servletOutputStream.close();
        } catch (IOException e) {
           log.error("获取验证码异常{}",e);
        }
    }

    private static String getCaptchaKey(HttpServletRequest request){
        String ipAddr = CommonUtil.getIpAddr(request);
        String header = request.getHeader("user-agent");
        String key = "user-service:"+CommonUtil.MD5(ipAddr+header);
        return key;
    }

    @ApiOperation("发送注册验证码")
    @GetMapping("/sendRegisterCode")
    public JsonData sendRegisterCode(HttpServletRequest request,
                                     @RequestParam(value="to",required = true) String to,
                                     @RequestParam(value = "captcha",required = true) String captcha) {
        String captchaKey = getCaptchaKey(request);
        String captchaRedis = stringRedisTemplate.opsForValue().get(captchaKey);
        if (StringUtils.isNotBlank(captcha) && StringUtils.isNotBlank(captchaRedis) && captchaRedis.equals(captcha)) {
            return notifyService.sendCode(SendCodeType.USER_REGISTER_MAIL, to);
        } else {
            return JsonData.buildResult(BizCodeEnum.CODE_CAPTCHA_ERROR);
        }
    }
}
